/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MatLab;

import java.util.logging.Level;
import java.util.logging.Logger;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;

/**
 *
 * @author Usuario
 */
public class MatLabAnalisis {

    private String commands_part1 = ""
            + "close all\n"
            + "clear all\n"
            + "clc\n"
            + "\n"
            + "cd('";
    private String commands_part2 = ""
            + "')\n"
            + "% ------------------------------------------------------------\n"
            + "%                 DEFINICIÓN DE VARIABLES \n"
            + "% ------------------------------------------------------------\n"
            + "\n"
            + "nombre_arch = '";
    private String commands_part3 = ""
            + "';\n"
            + "nombre_pdf  = nombre_arch(1:end-7);\n"
            + "nombre_pdf  = [nombre_pdf '.txt'];\n"
            + " \n"
            + "archivo     = load(nombre_arch);\n"
            + "archivo_pdf = load(nombre_pdf);\n"
            + "Num_cpo     = nombre_arch(end-5:end-4);\n"
            + "nombre_dcm  = ['dcm' Num_cpo];  \n"
            + "Num_cpo     = str2double(Num_cpo);\n"
            + "\n"
            + "UM_total    = archivo_pdf(2,Num_cpo);\n"
            + "Y           = archivo_pdf(2,Num_cpo);                                         % TAMAÑO DE CAMPO\n"
            + "X           = archivo_pdf(2,Num_cpo);\n"
            + "\n"
            + "Y           = Y/10;\n"
            + "X           = X/10;\n"
            + "fuga        = 0.035;\n"
            + "for i=1:length(Y),\n"
            + "    TC_eff(i)= 2*X(i)*Y(i)/(X(i)+Y(i));         % TAMAÑO DE CAMPO EFECTIVO\n"
            + "end\n"
            + "\n"
            + "DFI         = 100;\n"
            + "SSD         = 94.31;\n"
            + "z           = DFI - SSD;                        % profundidad en cm\n"
            + "\n"
            + "% peso        = [1 1 1 1];\n"
            + "% normalizacion   = 100;\n"
            + "% TDOSE           = 100;\n"
            + "% TPERCENT        = 200;\n"
            + "% numero_campos   = 1;\n"
            + "\n"
            + "%% Factor Sp\n"
            + "% Factores Sp vs indice de calidad del haz sacado del paper -A table of phantom scatter factors of photon beams as a\n"
            + "% function of the quality index and field size- de Storchi y Van Gasteren\n"
            + "%Ojo, estos factores Sp estan medidos a 10 cm de prof\n"
            + "%Para los TC 2 y 3 cm se extrapolo con una cubica los Sp de los TC entre 4\n"
            + "%y 10 cm\n"
            + "\n"
            + "tabla_Sp =[0.787	0.815	0.836	0.855	0.869	0.881	0.888	0.893	0.893	0.893	0.890\n"
            + "0.824	0.846	0.864	0.879	0.891	0.901	0.907	0.913	0.914	0.915	0.915\n"
            + "0.858	0.875	0.889	0.901	0.911	0.919	0.925	0.930	0.932	0.934	0.934\n"
            + "0.890	0.902	0.912	0.921	0.929	0.936	0.942	0.947	0.951	0.954	0.957\n"
            + "0.917	0.926	0.934	0.941	0.948	0.953	0.958	0.962	0.965	0.968	0.970\n"
            + "0.942	0.948	0.953	0.958	0.962	0.966	0.970	0.973	0.976	0.979	0.981\n"
            + "0.965	0.968	0.971	0.973	0.976	0.978	0.980	0.983	0.985	0.987	0.989\n"
            + "0.984	0.985	0.986	0.987	0.989	0.990	0.991	0.992	0.993	0.994	0.994\n"
            + "1.000	1.000	1.000	1.000	1.000	1.000	1.000	1.000	1.000	1.000	1.000\n"
            + "1.030	1.027	1.024	1.021	1.019	1.016	1.014	1.013	1.011	1.010	1.009\n"
            + "1.052	1.047	1.042	1.038	1.034	1.030	1.026	1.023	1.020	1.018	1.016\n"
            + "1.067	1.061	1.056	1.051	1.047	1.042	1.037	1.033	1.029	1.025	1.021\n"
            + "1.079	1.074	1.069	1.063	1.058	1.053	1.047	1.042	1.036	1.030	1.024\n"
            + "1.099	1.091	1.083	1.076	1.068	1.061	1.054	1.048	1.041	1.035	1.028\n"
            + "1.108	1.100	1.093	1.085	1.077	1.070	1.062	1.055	1.047	1.039	1.032\n"
            + "1.115	1.108	1.101	1.093	1.085	1.077	1.069	1.061	1.052	1.044	1.035\n"
            + "1.121	1.115	1.107	1.100	1.092	1.084	1.075	1.066	1.057	1.047	1.037\n"
            + "1.128	1.121	1.114	1.106	1.098	1.089	1.080	1.070	1.060	1.050	1.040\n"
            + "1.134	1.127	1.120	1.112	1.103	1.094	1.084	1.074	1.064	1.053	1.042\n"
            + "1.141	1.134	1.125	1.117	1.108	1.098	1.088	1.078	1.067	1.056	1.044\n"
            + "1.148	1.140	1.131	1.122	1.112	1.102	1.091	1.081	1.070	1.058	1.047\n"
            + "1.156	1.146	1.137	1.126	1.116	1.105	1.094	1.083	1.072	1.061	1.049\n"
            + "1.164	1.153	1.142	1.131	1.120	1.108	1.097	1.086	1.074	1.063	1.051\n"
            + "1.172	1.160	1.147	1.135	1.123	1.111	1.099	1.088	1.076	1.065	1.054];\n"
            + "\n"
            + "TC_tabla_Sp=[2:10,12:2:40];\n"
            + "IC_tabla_Sp=0.6:0.02:0.8;\n"
            + "\n"
            + "%% Datos: Equipo 4 6MV\n"
            + "    \n"
            + "    d_max=1.6;\n"
            + "    \n"
            + "    TC_Scp=[0	2	3	4	5	6 8	10	12	15	18	20	25	30	35 40 60];\n"
            + "    Scp=[0.840	0.883	0.904	0.925	0.943	0.957	0.978	1.000	1.013 ...\n"
            + "        1.031	1.044	1.050	1.070	1.079	1.099	1.089	1.143];\n"
            + "    \n"
            + "    PDD=[42.06	44.16	44.58	45.40	46.26	47.07	48.78	50.42	52.24	55.05	57.81	59.21	62.83	65.98	68.47	70.07	82.13\n"
            + "50.46	51.36	52.00	52.61	53.03	54.18	55.88	57.28	59.41	61.94	64.56	65.91	69.55	72.54	74.73	76.30	87.83\n"
            + "68.59	70.80	70.43	70.77	71.66	71.84	72.97	74.39	75.67	78.06	79.62	80.74	83.47	85.26	86.76	86.12	92.60\n"
            + "82.94	84.25	83.67	83.95	84.16	84.82	85.51	86.42	87.37	88.78	89.97	90.73	92.44	93.56	94.27	92.74	95.91\n"
            + "90.80	91.87	91.43	91.54	91.85	92.11	92.71	93.09	93.78	94.67	95.55	95.82	96.96	97.42	97.86	97.02	98.91\n"
            + "96.07	96.38	96.20	96.26	96.29	96.15	96.94	96.96	97.48	98.00	98.19	98.59	99.18	99.42	99.66	99.18	100.12\n"
            + "99.00	98.82	98.68	98.67	98.47	98.60	98.78	99.00	99.19	99.49	99.56	99.95	100.19	100.12	100.35	100.00	100.41\n"
            + "99.61	99.93	99.80	99.83	99.93	99.78	99.88	99.73	99.80	100.00	100.02	100.23	100.19	100.14	100.35	100.20	100.36\n"
            + "100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00	100.00\n"
            + "99.73	99.63	99.73	99.80	99.73	99.88	99.90	99.58	99.53	99.49	99.36	99.65	99.47	99.30	99.49	99.56	99.61\n"
            + "98.72	98.77	99.08	99.24	99.32	99.10	99.37	98.97	99.22	98.90	98.92	98.92	98.80	99.10	98.89	99.00	99.08\n"
            + "97.80	97.86	98.13	98.45	98.35	98.30	98.47	98.31	98.21	98.25	98.12	98.19	98.19	98.14	98.15	98.38	98.48\n"
            + "97.21	96.70	97.28	97.44	97.33	97.53	97.64	97.60	97.58	97.47	97.43	97.38	97.59	97.49	97.46	97.74	97.95\n"
            + "96.11	95.73	96.35	96.34	96.51	96.53	96.72	96.76	96.67	96.52	96.65	96.62	96.74	96.77	96.87	97.04	97.46\n"
            + "95.39	94.68	95.48	95.62	95.54	95.73	96.16	95.81	96.03	96.01	95.76	95.94	95.97	96.16	96.40	96.35	96.63\n"
            + "93.96	93.59	94.38	94.57	94.66	94.76	95.11	95.10	95.13	95.33	95.19	95.36	95.52	95.51	95.62	95.66	95.93\n"
            + "92.34	92.36	93.21	93.71	93.79	93.93	94.14	94.24	94.30	94.38	94.30	94.43	94.77	94.74	94.84	94.94	95.39\n"
            + "91.10	91.29	92.03	92.53	92.65	92.83	93.24	93.19	93.44	93.62	93.52	93.58	93.83	93.86	94.16	94.25	94.75\n"
            + "89.93	90.24	91.11	91.59	91.90	92.01	92.56	92.45	92.68	92.89	92.76	92.89	93.11	93.21	93.54	93.59	94.15\n"
            + "89.16	88.97	90.13	90.39	90.78	90.99	91.52	91.52	91.65	91.87	92.23	92.16	92.29	92.49	92.64	92.92	93.76\n"
            + "87.76	87.97	88.99	89.48	89.81	90.14	90.69	90.76	90.99	91.26	91.32	91.38	91.68	91.91	92.16	92.17	92.91\n"
            + "86.25	86.88	87.79	88.42	88.82	89.16	89.79	89.73	90.21	90.31	90.56	90.68	90.99	91.05	91.29	91.38	92.24\n"
            + "85.08	85.67	86.74	87.41	87.85	88.11	88.94	89.02	89.42	89.65	89.76	89.67	90.19	90.40	90.76	90.66	91.47\n"
            + "84.15	84.60	85.84	86.55	86.97	87.32	88.02	88.09	88.45	88.78	89.07	89.08	89.49	89.57	90.03	89.97	90.93\n"
            + "82.93	83.62	84.79	85.47	86.03	86.37	87.17	87.38	87.76	87.85	88.07	88.38	88.75	88.81	89.20	89.35	90.55\n"
            + "82.04	82.62	83.82	84.61	85.01	85.54	86.53	86.52	86.95	87.27	87.40	87.62	87.91	88.21	88.32	88.66	89.77\n"
            + "80.94	81.56	82.87	83.65	84.16	84.64	85.42	85.71	86.07	86.39	86.74	86.89	87.21	87.54	87.88	87.95	89.20\n"
            + "79.67	80.65	81.72	82.72	83.09	83.55	84.49	84.93	85.24	85.59	85.91	86.01	86.72	86.82	87.00	87.22	88.54\n"
            + "78.52	79.70	80.72	81.74	82.19	82.85	83.62	83.97	84.48	85.12	85.27	85.38	85.83	86.17	86.47	86.53	87.67\n"
            + "77.63	78.61	79.80	80.95	81.25	81.97	82.98	83.33	83.75	84.12	84.43	84.67	85.13	85.33	85.79	85.89	87.31\n"
            + "76.45	77.51	78.80	79.79	80.37	80.92	81.96	82.33	82.96	83.37	83.53	83.84	84.39	84.63	85.08	85.22	86.71\n"
            + "75.90	76.70	78.12	78.96	79.60	80.15	81.26	81.54	82.15	82.66	82.91	83.31	83.71	84.05	84.42	84.53	86.03\n"
            + "74.65	75.70	77.02	78.05	78.60	79.25	80.21	80.69	81.20	81.84	82.16	82.38	82.80	83.26	83.54	83.84	85.44\n"
            + "73.71	74.75	76.12	76.94	77.73	78.35	79.46	79.93	80.49	81.13	81.40	81.67	82.13	82.68	82.99	83.15	84.77\n"
            + "72.74	73.70	75.15	76.01	76.76	77.48	78.61	79.09	79.73	80.35	80.58	81.00	81.52	81.93	82.13	82.42	84.09\n"
            + "71.79	72.80	74.23	75.12	75.86	76.70	77.73	78.33	78.95	79.52	79.85	80.23	80.84	81.35	81.55	81.68	83.42\n"
            + "70.83	71.87	73.38	74.29	75.08	75.83	76.88	77.50	78.19	78.84	79.23	79.40	80.00	80.51	80.94	81.01	82.76\n"
            + "70.02	70.82	72.45	73.40	74.07	74.96	76.28	76.84	77.36	78.13	78.43	78.80	79.42	79.89	80.26	80.35	82.13\n"
            + "68.86	70.01	71.53	72.57	73.31	74.13	75.23	75.78	76.60	77.38	77.69	78.14	78.77	79.12	79.55	79.71	81.58\n"
            + "67.77	69.13	70.60	71.71	72.49	73.28	74.60	75.17	75.89	76.53	77.08	77.36	78.10	78.52	78.75	79.04	81.05\n"
            + "67.07	68.20	69.81	70.82	71.64	72.48	73.87	74.44	75.23	75.87	76.34	76.61	77.36	77.84	78.17	78.35	80.34\n"
            + "65.77	67.36	68.71	70.03	70.67	71.61	72.85	73.60	74.22	74.97	75.59	75.84	76.54	77.12	77.35	77.61	79.72\n"
            + "64.99	66.41	67.93	69.12	69.89	70.81	72.10	72.84	73.56	74.41	74.84	75.24	75.86	76.38	76.91	76.94	78.98\n"
            + "64.33	65.41	67.13	68.12	69.00	69.79	71.32	71.91	72.88	73.68	74.02	74.46	75.16	75.68	76.13	76.30	78.41\n"
            + "63.40	64.72	66.26	67.35	68.17	69.26	70.54	71.25	72.14	73.05	73.45	73.75	74.61	75.13	75.44	75.66	77.76\n"
            + "62.45	63.74	65.46	66.59	67.47	68.34	69.81	70.44	71.24	72.22	72.67	73.05	73.86	74.28	74.77	75.04	77.30\n"
            + "61.77	63.09	64.71	65.73	66.67	67.64	68.91	69.75	70.53	71.49	72.14	72.37	73.23	73.82	74.11	74.38	76.70\n"
            + "60.66	62.30	63.81	65.14	65.91	66.82	68.28	69.07	69.82	70.83	71.43	71.82	72.51	73.10	73.67	73.74	76.07\n"
            + "60.31	61.56	63.19	64.33	65.11	66.14	67.67	68.26	69.18	70.05	70.77	71.05	71.96	72.52	72.84	73.10	75.54\n"
            + "59.00	60.70	62.36	63.54	64.60	65.19	66.77	67.60	68.49	69.42	70.01	70.35	71.31	71.75	72.15	72.53	75.02\n"
            + "58.43	59.86	61.64	62.91	63.78	64.57	66.12	66.96	67.78	68.76	69.35	69.92	70.65	71.19	71.49	71.91	74.45\n"
            + "56.87	58.21	59.79	60.94	61.74	62.85	64.32	65.17	66.12	67.15	67.84	68.11	69.14	69.50	70.23	70.30	72.83\n"
            + "54.82	56.38	57.97	59.02	60.07	60.97	62.64	63.33	64.43	65.42	66.14	66.57	67.55	68.18	68.51	68.66	71.26\n"
            + "52.73	54.61	56.04	57.25	58.25	59.18	60.70	61.74	62.74	63.84	64.61	64.93	65.92	66.54	67.09	67.16	69.83\n"
            + "51.15	52.82	54.40	55.56	56.57	57.45	59.09	60.02	61.05	62.24	62.98	63.42	64.27	64.96	65.50	65.70	68.48\n"
            + "49.43	51.01	52.65	53.74	54.80	55.78	57.49	58.41	59.49	60.53	61.36	61.67	62.83	63.45	64.00	64.19	67.13\n"
            + "47.95	49.62	51.10	52.14	53.20	54.23	55.83	56.79	57.87	59.12	59.85	60.22	61.36	62.06	62.68	62.68	65.53\n"
            + "46.26	48.11	49.45	50.64	51.58	52.51	54.16	55.20	56.33	57.58	58.43	58.88	59.91	60.54	61.09	61.22	64.15\n"
            + "44.53	46.64	47.90	49.07	50.15	51.16	52.75	53.77	54.93	56.17	57.01	57.41	58.51	59.22	59.83	59.86	62.82\n"
            + "43.62	45.20	46.63	47.71	48.64	49.54	51.26	52.38	53.39	54.69	55.61	55.88	56.97	57.71	58.47	58.60	61.73\n"
            + "42.21	43.76	45.28	46.24	47.33	48.26	49.88	50.88	52.02	53.37	54.37	54.75	55.77	56.57	57.21	57.32	60.50\n"
            + "40.65	42.39	43.83	44.89	45.95	46.77	48.42	49.51	50.53	52.08	52.95	53.38	54.37	55.15	55.93	55.93	59.02\n"
            + "39.64	41.11	42.61	43.56	44.59	45.44	47.06	48.24	49.28	50.60	51.72	52.04	53.19	54.02	54.70	54.68	57.95\n"
            + "38.38	39.88	41.36	42.26	43.35	44.14	45.77	46.86	47.96	49.35	50.39	50.66	51.80	52.75	53.40	53.40	56.65\n"
            + "37.02	38.68	40.03	40.98	42.04	42.87	44.48	45.61	46.78	48.02	49.06	49.39	50.52	51.50	52.08	52.08	55.35\n"
            + "35.76	37.40	38.86	39.90	40.93	41.57	43.29	44.34	45.48	46.73	47.87	48.16	49.40	50.31	50.98	50.91	54.27\n"
            + "34.65	36.24	37.66	38.57	39.67	40.45	42.03	43.09	44.24	45.53	46.59	46.98	48.23	49.13	49.71	49.75	53.14\n"
            + "33.85	35.19	36.61	37.46	38.45	39.28	40.81	41.86	43.11	44.36	45.51	45.90	47.07	47.95	48.49	48.65	52.09\n"
            + "32.74	34.10	35.51	36.41	37.36	38.18	39.67	40.74	41.91	43.27	44.25	44.70	45.89	46.85	47.43	47.47	50.85\n"
            + "31.75	33.17	34.52	35.25	36.37	37.00	38.58	39.71	40.81	42.17	43.22	43.55	44.76	45.59	46.31	46.31	49.64\n"
            + "30.60	32.31	33.42	34.34	35.30	35.96	37.58	38.58	39.76	41.12	42.17	42.46	43.70	44.71	45.18	45.32	48.69\n"
            + "28.74	30.24	31.44	32.30	33.24	34.03	35.46	36.42	37.60	38.96	39.97	40.32	41.63	42.39	43.07	43.19	46.58\n"
            + "27.32	28.52	29.72	30.41	31.32	31.99	33.50	34.51	35.59	36.86	37.93	38.30	39.51	40.39	41.07	41.21	44.71\n"
            + "25.60	26.90	28.00	28.66	29.60	30.24	31.65	32.65	33.71	35.01	36.10	36.46	37.58	38.52	39.19	39.16	42.49\n"
            + "24.03	25.32	26.35	27.02	27.90	28.51	29.90	30.83	31.80	33.16	34.27	34.62	35.66	36.66	37.28	37.26	40.56\n"
            + "22.95	23.97	24.93	25.52	26.25	26.97	28.25	29.19	30.13	31.48	32.55	32.89	33.94	34.90	35.54	35.60	38.91\n"
            + "21.37	22.51	23.45	24.09	24.84	25.39	26.69	27.60	28.59	29.83	30.92	31.19	32.35	33.32	33.81	33.88	37.13\n"
            + "20.26	21.25	22.18	22.66	23.46	24.00	25.26	26.15	27.07	28.37	29.30	29.63	30.69	31.62	32.19	32.13	35.16\n"
            + "19.15	20.12	20.95	21.44	22.15	22.60	23.85	24.68	25.61	26.81	27.90	28.09	29.20	30.04	30.62	30.62	33.68\n"
            + "17.91	19.00	19.71	20.13	20.91	21.32	22.51	23.33	24.21	25.47	26.41	26.70	27.70	28.62	29.19	29.08	31.98\n"
            + "16.77	17.96	18.61	19.10	19.84	20.25	21.32	22.08	22.96	24.23	25.06	25.44	26.38	27.25	27.84	27.75	30.57\n"
            + "5.32	7.56	7.61	8.80	9.14	9.55	9.42	9.58	10.46	11.83	11.55	12.81	13.15	13.53	14.29	14.42	16.50];\n"
            + "    TC_PDD=[0	2	3	4	5	6 8	10	12	15	18	20	25	30	35 40 60];\n"
            + "    prof_PDD=[0	0.2	0.4	0.6	0.8	1	1.2	1.4	1.6	1.8	2	2.2	2.4	2.6	2.8	3 3.2	3.4	3.6	3.8	4	4.2	4.4	4.6	4.8	5 ... \n"
            + "        5.2	5.4	5.6	5.8	6	6.2	6.4	6.6	6.8	7	7.2	7.4	7.6	7.8	8	8.2	8.4	8.6	8.8	9	9.2	9.4	9.6	9.8	10	10.5 ... \n"
            + "        11	11.5	12	12.5	13	13.5	14	14.5	15	15.5	16	16.5	17	17.5	18	18.5	19	19.5 ... \n"
            + "        20	21	22	23	24	25	26	27	28	29	30	40];\n"
            + "    \n"
            + "    shield_factor=0.9812;\n"
            + "    \n"
            + "    TPR_20_10 = 0.673; %sacado de las absolutas. Poner fecha de medicion!!!\n"
            + "    \n"
            + "    DR_0=1.01129; %tasa de dosis para 10x10 a prof del maximo (Dmax=1.6 cm) 26  06 2011 \n"
            + "    \n"
            + "    param=0.05;\n"
            + " \n"
            + "   \n"
            + "    %% \n"
            + "    \n"
            + "% % -----------------------------------------------------------------------    \n"
            + "%                     CALCULO DEL KERNEL\n"
            + "% % -----------------------------------------------------------------------    \n"
            + "\n"
            + "%Kernel obtenido del paper Photon pencil kernel parameterisation based on beam quality index\n"
            + "\n"
            + "poli_TPR=[1,TPR_20_10,(TPR_20_10)^2,(TPR_20_10)^3,(TPR_20_10)^4,(TPR_20_10)^5];\n"
            + "\n"
            + "A_1 = [0.0128018 -0.0577391 0.1790839 -0.2467955 0.1328192 -0.0194684]...\n"
            + "    *poli_TPR';\n"
            + "A_2 = [16.7815028 -279.4672663 839.0016549 -978.4915013 470.5317337 -69.2485573]...\n"
            + "    *poli_TPR';\n"
            + "A_3 = [-0.0889669 -0.2587584 0.7069203 -0.3654033 0.0029760 -0.0003786]...\n"
            + "    *poli_TPR';\n"
            + "A_4 = [0.0017089 -0.0169150 0.0514650 -0.0639530 0.0324490 -0.0049121]...\n"
            + "    *poli_TPR';\n"
            + "A_5 = [0.1431447 -0.2134626 0.5825546 -0.2969273 -0.0011436 0.0002219]...\n"
            + "    *poli_TPR';\n"
            + "B_1 = [-42.7607523 264.3424720 -633.4540368 731.5311577 -402.5280374 82.4936551]...\n"
            + "    *poli_TPR';\n"
            + "B_2 = [0.2428359 -2.5029336 7.6128101 -9.5273454 4.8249840 -0.7097852]...\n"
            + "    *poli_TPR';\n"
            + "B_3 = [-0.0910420 -0.2621605 0.7157244 -0.3664126 0.0000930 -0.0000232]...\n"
            + "    *poli_TPR';\n"
            + "B_4 = [0.0017284 -0.0172146 0.0522109 -0.0643946 0.0322177 -0.0047015]...\n"
            + "    *poli_TPR';\n"
            + "B_5 = [-30.4609625 354.2866078 -1073.2952368 1315.2670101 -656.3702845 96.5983711]...\n"
            + "    *poli_TPR';\n"
            + "a_1 = [-26.3337419 435.6865552 -1359.8342546 1724.6602381 -972.7565415 200.3468023]...\n"
            + "    *poli_TPR';\n"
            + "a_2= [-0.0065985 0.0242136 -0.0647001 0.0265272 0.0072169 -0.0020479]...\n"
            + "    *poli_TPR';%Chequear que es la version corregida!!!!!\n"
            + "b_1 = [-80.7027159 668.1710175 -2173.2445309 3494.2393490 -2784.4670834 881.2276510]...\n"
            + "    *poli_TPR';\n"
            + "b_2 = [3.4685991 -41.2468479 124.9729952 -153.2610078 76.5242757 -11.2624113]...\n"
            + "    *poli_TPR';\n"
            + "b_3 = [-39.6550497 277.7202038 -777.0749505 1081.5724508 -747.1056558 204.5432666]...\n"
            + "    *poli_TPR';\n"
            + "b_4 = [0.6514859 -4.7179961 13.6742202 -19.7521659 14.1873606 -4.0478845]...\n"
            + "    *poli_TPR';\n"
            + "b_5 = [0.4695047 -3.6644336 10.0039321 -5.1195905 -0.0007387 0.0002360]...\n"
            + "    *poli_TPR';\n"
            + "\n"
            + "% % -----------------------------------------------------------------------\n"
            + "a=a_1+a_2*z;                                                % Calculo de los parámetros con los coeficientes\n"
            + "b=b_1*(1-exp(b_2*sqrt(z^2+b_5^2)))*exp(b_3*z+b_4*z^2);\n"
            + "A=A_1*(1-exp(A_2*sqrt(z^2+A_5^2)))*exp(A_3*z+A_4*z^2)*a;\n"
            + "B=B_1*(1-exp(B_2*sqrt(z^2+B_5^2)))*exp(B_3*z+B_4*z^2)*b;\n"
            + "\n"
            + "KK          = zeros(200,200);                               % Hago un kernel mas chico porque no tiene sentido (PARA MI)\n"
            + "r           = zeros(200,200);\n"
            + "\n"
            + "x_calc=20/2+0.1+param; % me corro 1/2 mm en cada eje del punto me calculo para evitar la divergencia en r=0\n"
            + "y_calc=20/2+0.1+param;\n"
            + "\n"
            + "for i=1:200\n"
            + "    for j=1:200\n"
            + "        r(i,j)=sqrt((i/10-y_calc)^2+(j/10-x_calc)^2);\n"
            + "        KK(i,j)=(A*exp(-a*r(i,j))+B*exp(-b*r(i,j)))/r(i,j);\n"
            + "    end\n"
            + "end\n"
            + "\n"
            + "\n"
            + "  %% \n"
            + "% % -----------------------------------------------------------------------    \n"
            + "%                     CALCULO DE LA FLUENCIA\n"
            + "% % -----------------------------------------------------------------------    \n"
            + "\n"
            + "lado        = X*10/2;                                       % Paso al lado a mm\n"
            + "fluencia_1  = zeros(400,400);\n"
            + "for i = 0:lado                                      % Creo un mapa de fluencia cuadrado de lado=X\n"
            + "    for j = 0:lado\n"
            + "        aux_x1 = 200 + i;                                   % Centrado\n"
            + "        aux_y1 = 200 + j;\n"
            + "        aux_x2 = 200 - i;\n"
            + "        aux_y2 = 200 - j;\n"
            + "        fluencia_1(aux_x1,aux_y1)=1;   \n"
            + "        fluencia_1(aux_x1,aux_y2)=1;\n"
            + "        fluencia_1(aux_x2,aux_y1)=1;\n"
            + "        fluencia_1(aux_x2,aux_y2)=1;\n"
            + "    end\n"
            + "end\n"
            + "\n"
            + " %% \n"
            + "% % -----------------------------------------------------------------------    \n"
            + "%                               CONVOLUCION\n"
            + "% % -----------------------------------------------------------------------    \n"
            + "\n"
            + "\n"
            + "D1          = conv2(fluencia_1,KK);\n"
            + "D1_red      = D1(101:500,101:500);\n"
            + "\n"
            + "[x_KK,y_KK] = meshgrid(-100:1:100-1);\n"
            + "[xxx,yyy]   = meshgrid(-200:1:200-1);\n"
            + "\n"
            + "x_d     = 0:80;\n"
            + "% % % % perfil_calc  = D1(300:380,300)*100/max(D1(300:380,300));\n"
            + "\n"
            + " %% \n"
            + "\n"
            + "% --------------------------------------------\n"
            + "%                 carga de segmentos y distribucion\n"
            + "\n"
            + "index       = archivo(1,:);\n"
            + "x           = archivo(2:end,:);\n"
            + "aux         = size(x);\n"
            + "cant_s      = aux(2);\n"
            + "% ----------------------------------------------\n"
            + "%                 pesos\n"
            + "\n"
            + "pesos(1) = index(1);\n"
            + "for i=2:cant_s,\n"
            + "    pesos(i) = index(i)-index(i-1);\n"
            + "end\n"
            + "UM = pesos*UM_total;\n"
            + "\n"
            + "% ---------------------------------------------\n"
            + "%               creacion de matrices\n"
            + "% ---------------------------------------------\n"
            + "\n"
            + "Mizq        = zeros(cant_s,400,400);\n"
            + "Mder        = zeros(cant_s,400,400);\n"
            + "mlc_offset  = 1;\n"
            + "for k = 1:cant_s,\n"
            + "    for i=1:10,\n"
            + "        aux_izq     = (200 - 10*(x(i,k)))+mlc_offset;\n"
            + "        aux_der     = (200 + 10*(x(i+60,k)));\n"
            + "        for j = 1:400,\n"
            + "            if j<aux_izq\n"
            + "                indice_aux                          = i*10-9;\n"
            + "                indice_aux2                         = i*10;\n"
            + "                Mizq(k,indice_aux:indice_aux2,j)    = 1;\n"
            + "            end\n"
            + "            if j>aux_der\n"
            + "                indice_aux                          = i*10-9;\n"
            + "                indice_aux2                         = i*10;\n"
            + "                Mder(k,indice_aux:indice_aux2,j)=1;\n"
            + "            end\n"
            + "        end\n"
            + "    end\n"
            + "    cont    = 0;\n"
            + "    for i=11:50,\n"
            + "        aux_izq     = (200 - 10*(x(i,k)))+mlc_offset;\n"
            + "        aux_der     = (200 + 10*(x(i+60,k)));\n"
            + "        cont        = cont + 1;\n"
            + "        for j = 1:400,\n"
            + "            if j<aux_izq\n"
            + "                indice_aux                      = cont*5+96;\n"
            + "                indice_aux2                     = cont*5+100;\n"
            + "                Mizq(k,indice_aux:indice_aux2,j)= 1;\n"
            + "            end\n"
            + "            if j>aux_der\n"
            + "                indice_aux                      = cont*5+96;\n"
            + "                indice_aux2                     = cont*5+100;\n"
            + "                Mder(k,indice_aux:indice_aux2,j)=1;\n"
            + "            end\n"
            + "        end\n"
            + "    end\n"
            + "    cont    = 0;\n"
            + "    for i=51:60,\n"
            + "        aux_izq     = (200 - 10*(x(i,k)))+mlc_offset;\n"
            + "        aux_der     = (200 + 10*(x(i+60,k)));\n"
            + "        cont        = cont + 1;\n"
            + "        for j = 1:400,\n"
            + "            if j<aux_izq\n"
            + "                indice_aux                      = cont*10+291;\n"
            + "                indice_aux2                     = cont*10+300;\n"
            + "                Mizq(k,indice_aux:indice_aux2,j)= 1;\n"
            + "            end\n"
            + "            if j>aux_der\n"
            + "                indice_aux                      = cont*10+291;\n"
            + "                indice_aux2                     = cont*10+300;                \n"
            + "                Mder(k,indice_aux:indice_aux2,j)= 1;\n"
            + "            end\n"
            + "        end\n"
            + "    end\n"
            + "end\n"
            + "\n"
            + "% % % % % % % % % % % -------------------------------------------------\n"
            + "% %                     solo para graficar MLC\n"
            + "\n"
            + "kizq= 3;\n"
            + "kder= 7;\n"
            + "\n"
            + "figure()\n"
            + "hold on\n"
            + "clear aux\n"
            + "for i=1:cant_s,\n"
            + "    subplot(3,3,i)\n"
            + "    aux(:,:) = Mizq(i,:,:)*kizq + Mder(i,:,:)*kder;\n"
            + "    titulo = num2str(round(UM(i)));\n"
            + "    aux      = imrotate(aux,180);\n"
            + "    image(aux)\n"
            + "    title([titulo,' UM'])\n"
            + "    colormap(jet(10))\n"
            + "    axis image        % Set aspect ratio to obtain square pixels\n"
            + "    axis([100 300 100 300])\n"
            + "    grid on\n"
            + "end\n"
            + "print -djpeg -r300 ";
    String commands_part4 = "_pic1\n"
            + "hold off\n"
            + "% % % % % % % % % % % -------------------------------------------------\n"
            + "\n"
            + "M = Mizq+Mder;\n"
            + "for i=1:cant_s,\n"
            + "    for j=1:400,\n"
            + "        for k=1:400,\n"
            + "            if M(i,j,k)==1\n"
            + "               M(i,j,k)=fuga;\n"
            + "            else \n"
            + "               M(i,j,k)=1;\n"
            + "            end\n"
            + "        end\n"
            + "    end\n"
            + "end\n"
            + "\n"
            + "fluencia = zeros(400,400);\n"
            + "for i=1:cant_s,\n"
            + "    aux =zeros(400,400);\n"
            + "    aux(:,:) = M(i,:,:);\n"
            + "    aux = aux*pesos(i);\n"
            + "%     pause\n"
            + "    fluencia = fluencia + aux;\n"
            + "end\n"
            + "\n"
            + "D2      = conv2(fluencia,KK);\n"
            + "D2      = imrotate(D2,180);\n"
            + "D2_red  = D2(99:498,99:498);\n"
            + "\n"
            + "% -------------------------------------------\n"
            + "%             graficacion\n"
            + "\n"
            + "% % % % kizq= 3;\n"
            + "% % % % kder= 6;\n"
            + "% % % % Mizq = Mizq *kizq;\n"
            + "% % % % Mder = Mder *kder;\n"
            + "% % % % M = Mizq+Mder;\n"
            + "% % % % \n"
            + "% % % % figure()\n"
            + "% % % % hold on\n"
            + "% % % % for i=1:cant_s,\n"
            + "% % % %     subplot(3,3,i)\n"
            + "% % % %     \n"
            + "% % % %     aux =zeros(80,80);\n"
            + "% % % %     aux(:,:) = M(i,:,:);\n"
            + "% % % %     zoom = 33:47;\n"
            + "% % % %     titulo = num2str(round(UM(i)));\n"
            + "% % % %     image(zoom,zoom,aux)\n"
            + "% % % %     title([titulo,' UM'])\n"
            + "% % % %     colormap(jet(10))\n"
            + "% % % %     axis image        % Set aspect ratio to obtain square pixels\n"
            + "% % % %     grid on\n"
            + "% % % % end\n"
            + "% % % % hold off\n"
            + "\n"
            + "% ------------------------------------------------------\n"
            + "%             graficacion de fluencia\n"
            + "% \n"
            + "% % figure()\n"
            + "% % hold on\n"
            + "% % image(fluencia*255)\n"
            + "% % % colormap(winter)\n"
            + "% % axis image        % Set aspect ratio to obtain square pixels\n"
            + "% % axis([100 300 100 300])\n"
            + "% % grid on\n"
            + "\n"
            + "\n"
            + "% figure()\n"
            + "% meshc(xxx,yyy,fluencia);\n"
            + "% \n"
            + "% figure()\n"
            + "% meshc(xxx,yyy,D2_red);\n"
            + "% axis([-200 200 -200 200 0 2])\n"
            + "\n"
            + "% % % norm     = D2_red*100/D2_red(200,200);\n"
            + "% % % norm_aux = D2_red/1000;\n"
            + "% % % dicomwrite(norm_aux, 'AAAAA')\n"
            + "\n"
            + "\n"
            + "norm     = D2_red*100/D2_red(200,200);\n"
            + "\n"
            + "\n"
            + "figure()\n"
            + "imshow(norm,[])\n"
            + "colormap(jet)\n"
            + "colorbar\n"
            + "title(colorbar, 'Dosis')\n"
            + "axis on\n"
            + "axis([120 280 120 280])\n"
            + "\n"
            + "print -djpeg -r300 ";
    String commands_part5 = "_pic2\n"
            + "col_max     = max(D2_red);\n"
            + "fil_max     = max(D2_red');\n"
            + "[MAX,I1]    = max(col_max);\n"
            + "[MAX,I2]    = max(fil_max);\n"
            + "%%\n"
            + "\n"
            + "% % % % perfil      = load('Profile_Data 10x10 MLC.txt');\n"
            + "% % % % perfil_TPS  = perfil(:,2)*100/max(perfil(:,2));\n"
            + "% % % % figure()\n"
            + "% % % % hold on\n"
            + "% % % % plot(x_d,perfil_calc,'b')\n"
            + "% % % % plot(perfil(:,1),perfil_TPS,'r')\n"
            + "% % % % legend('Calculado','Cat3D')\n"
            + "% % % % hold off\n"
            + "\n"
            + "\n"
            + "\n"
            + "%%\n"
            + "% % % %%                  CALCULO DE DOSIS\n"
            + "% % % % % % % % % % ------------------------------------------r\n"
            + "% Matrizz = zeros(200,200);\n"
            + "% for i=1:cant_s,\n"
            + "%     Matrizz(:,:)    = M(i,101:300,101:300);\n"
            + "%     dosis_conf(i)   = trace(Matrizz*KK');\n"
            + "%     dosis_open      = trace(ones(size(Matrizz))*KK');\n"
            + "%     factor_pb(i)    = dosis_conf(i)/dosis_open;\n"
            + "% end\n"
            + "% \n"
            + "D1_KK           = D1_red(101:300,101:300);\n"
            + "D2_KK           = D2_red(101:300,101:300);\n"
            + "dosis_conf      = trace(D2_KK*KK');\n"
            + "dosis_open      = trace(D1_KK*KK');\n"
            + "\n"
            + "factor_pb_1     = dosis_conf/dosis_open;\n"
            + "factor_pb_2     = D2_red(200,200)/D1_red(200,200);\n"
            + "\n"
            + "\n"
            + "%calculo UM independiente campo 1\n"
            + "   \n"
            + "    PDD_1       = interp2(TC_PDD, prof_PDD, PDD, TC_eff(1)*DFI/(DFI+z), z);\n"
            + "    TC_dmax     = TC_eff(1)*(DFI+d_max)/(DFI+z);\n"
            + "    Sp_d_max    = interp2(IC_tabla_Sp,TC_tabla_Sp,tabla_Sp,TPR_20_10,TC_dmax)*...\n"
            + "        interp2(TC_PDD, prof_PDD, PDD, 10, 10)/interp2(TC_PDD, prof_PDD, PDD, TC_dmax, 10);\n"
            + "    Sp_prof     = interp2(IC_tabla_Sp,TC_tabla_Sp,tabla_Sp,TPR_20_10,TC_eff(1))*...\n"
            + "        interp2(TC_PDD, prof_PDD, PDD, 10, 10)/interp2(TC_PDD, prof_PDD, PDD, TC_eff(1), 10);\n"
            + "    TMR_1       = PDD_1/100*Sp_d_max/Sp_prof*((DFI+z)/(DFI+d_max))^2;\n"
            + "    Scp_1       = interp1(TC_Scp, Scp, TC_eff(1));\n"
            + "    Sp_tc_ref   = interp2(IC_tabla_Sp,TC_tabla_Sp,tabla_Sp,TPR_20_10,10*(DFI+d_max)/DFI)*...\n"
            + "        interp2(TC_PDD, prof_PDD, PDD, 10, 10)/interp2(TC_PDD, prof_PDD, PDD, 10*(DFI+d_max)/DFI, 10);\n"
            + "        \n"
            + "    DR_indep_campo_1    = DR_0*TMR_1*Scp_1/Sp_tc_ref*((DFI+d_max)/DFI)^2*factor_pb_1;\n"
            + "    DR_indep_campo_2    = DR_0*TMR_1*Scp_1/Sp_tc_ref*((DFI+d_max)/DFI)^2*factor_pb_2;\n"
            + "   \n"
            + "    D111                = DR_indep_campo_1*UM_total;\n"
            + "    D222                = DR_indep_campo_2*UM_total;\n"
            + "    \n"
            + "    norm_aux = D2_red*D111/(D2_red(200,200)*65535);\n"
            + "    dicomwrite(norm_aux, nombre_dcm)\n"
            + "\n"
            + "    \n"
            + "    \n"
            + "% dosis_campo_1=peso(1)*100/normalizacion*100*TDOSE/TPERCENT;\n"
            + "% % % \n"
            + "% % %     UM_indep_1=dosis_campo_1/DR_indep_campo_1;\n"
            + "% % %     sesgo_1=(UM_indep_1/UM(1)-1)*100;\n";

    public void usarMatlab(String ruta, String nombreArchivo, javax.swing.JFrame padre) throws MatlabConnectionException, MatlabInvocationException {

        MatlabProxyFactory factory = new MatlabProxyFactory();
        MatlabProxy proxy = null;
        try {
            proxy = factory.getProxy();
        } catch (MatlabConnectionException ex) {
            throw ex;
        }
        try {
            proxy.eval(commands_part1 + ruta + "\\Otros\\" + commands_part2 + nombreArchivo + commands_part3
                    + (nombreArchivo.substring(0, nombreArchivo.length() - 4)) + commands_part4 + (nombreArchivo.substring(0, nombreArchivo.length() - 4)) + commands_part5);
        } catch (MatlabInvocationException ex) {
            throw ex;
        }
        try {
            proxy.exit();

        } catch (MatlabInvocationException ex) {
            throw ex;
        }

    }
}
